package com.youlu.myapplication;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.youlu.myapplication.databinding.ActivityMainBinding;

import java.io.File;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mBinding;
    private boolean DEBUG = true;
    private static final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private PngHandler mPngHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        mBinding = ActivityMainBinding.inflate(inflater);
        setContentView(mBinding.getRoot());
        verifyStoragePermissions(this);
    }

    public void verifyStoragePermissions(Activity activity) {


        int REQUEST_EXTERNAL_STORAGE = 1;
        String[] PERMISSIONS_STORAGE = {
                "android.permission.READ_EXTERNAL_STORAGE",
                "android.permission.WRITE_EXTERNAL_STORAGE"};
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            } else {
                if (null == mPngHandler) {
                    mPngHandler = new PngHandler();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (null == mPngHandler) {
            mPngHandler = new PngHandler();
        }
    }

    Bitmap bitmap;

    public void loadPng(View view) {

        if (null != mPngHandler) {
            File file = new File(Environment.getExternalStorageDirectory(), "ic_launcher.png");
            //ic_launcher
            mPngHandler.init(file.getAbsolutePath());
            try {
                int height = mPngHandler.getHeight();
                int width = mPngHandler.getWidth();
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                mPngHandler.updateBitmap(bitmap);
                mBinding.png.setImageBitmap(bitmap);

                if (DEBUG) {
                    Log.i(TAG, "height is " + mBinding.png.getHeight());
                    Log.i(TAG, "width is " + mBinding.png.getWidth());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


    private native void nativeTestBitmap(Bitmap bitmap);

    public void testBitmap(View view) {

        PngHandler pngHandler = new PngHandler();
        Bitmap bitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.ARGB_8888);
        pngHandler.testBitmap(bitmap);
        mBinding.image.setImageBitmap(bitmap);
    }
}